package views.components
{
	import caurina.transitions.Equations;
	import caurina.transitions.properties.ColorShortcuts;
	import caurina.transitions.Tweener;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Timer;
	import models.MainModel;
	import models.types.SectionType;
	import models.types.SubsectionType;
	import caurina.transitions.properties.DisplayShortcuts;

	public class Section extends Sprite
	{
		
		private var _data:SectionType;
		private var _mc:MovieClip;
		
		private var _id:int;

		private var _subsections_container:MovieClip;
		private var _subsections:/*Subsection*/Array;
		
		private var shape_mask:MovieClip;
		
		private var _masks:Array = [];
		
		private var close_timer:Timer;

		public function Section(mc:DisplayObject, data:SectionType):void
		{	
			DisplayShortcuts.init();
			ColorShortcuts.init();
			
			close_timer = new Timer(1);
			close_timer.addEventListener(TimerEvent.TIMER, closeHandler);
			
			_data = data;
			_id = _data.id;
			
			_mc = MovieClip(mc);
			
			if (_data.total_subsections != 0)
			{
				_mc.Border.gotoAndStop(_data.total_subsections);
			}
			
			fillTitle();
			
			_mc.Area.width = _mc.Title.width;
			
			_subsections_container = _mc.Border.Subsections;
			
			Tweener.addTween(_mc.Border.Shape, { _color: Main.instance.colors[_id] } );
			
			_mc.Area.buttonMode = true;
			_mc.Area.addEventListener(MouseEvent.MOUSE_OVER, mouseHandler);
			_mc.Area.addEventListener(MouseEvent.CLICK, mouseHandler);
			
			createSubsections();
			
		}
		
		public function show(val:Number):void
		{
			Tweener.addTween(_mc.Tail, {delay:val, time: 0.3, _frame:10, onComplete:titleFadeIn, transition:Equations.easeNone } );
		}
		
		private function fillTitle():void
		{
			_mc.Title.title_txt.autoSize = TextFieldAutoSize.LEFT;
			_mc.Title.title_txt.text = _data.title;
		}
		
		private function mouseHandler(e:MouseEvent):void 
		{
			switch (e.type)
			{
				case MouseEvent.MOUSE_OVER:
					if (_data.total_subsections == 0)
					{
						break;
					}
					Tweener.addTween(_mc.Tail, { time: 0.1, _frame:20, onComplete:maskAnimateIn, transition:Equations.easeNone } );
					Main.instance.stage.addEventListener(MouseEvent.MOUSE_MOVE, closeHandler);
					Main.instance.stage.addEventListener(Event.ENTER_FRAME, closeHandler);
					Main.instance.stage.addEventListener(Event.MOUSE_LEAVE, closeBranch);
					close_timer.start();
					break;
				case MouseEvent.CLICK:
					navigateToURL(new URLRequest(_data.url), "_self");
					break;
			}
		}
		
		private function closeBranch(e:Event = null):void
		{
			close_timer.stop();
			Main.instance.stage.removeEventListener(MouseEvent.MOUSE_MOVE, closeHandler);
			Main.instance.stage.removeEventListener(Event.ENTER_FRAME, closeHandler);
			Tweener.removeTweens(_mc.Border.Shape.Mask);
			_mc.Border.Shape.Mask.gotoAndStop(1);
			//Tweener.addTween(_mc.Border.Shape.Mask, { _frame:1, transition:Equations.easeNone } );
			hideSubsections();
			enlargeTail();
		}
		
		private function closeHandler(e:* = null):void
		{
			var substrate:MovieClip = _mc.Border.Substrate;
			var p:Point = substrate.globalToLocal(new Point(Main.instance.mouseX, Main.instance.mouseY));
			if (p.x > substrate.width + 3 || p.x < -3 || p.y > substrate.height + 3 || p.y < -3)
			{
				closeBranch();
			}
		}
		
		private function maskAnimateIn():void 
		{
			showSubsections();
			Tweener.addTween(_mc.Border.Shape.Mask, { time: 0.5, _frame:25, onComplete:activateSubsections, transition:Equations.easeNone } );
		}
		
		private function titleFadeIn():void
		{
			
			Tweener.addTween(_mc.Title, {time: 0.4, alpha:1} );
		}
		
		private function enlargeTail():void
		{
			Tweener.addTween(_mc.Tail, { time: 0.1, _frame:10, onComplete:titleFadeIn, transition:Equations.easeNone } );
		}
		
		private function activateSubsections():void
		{
			for (var i:int = 0 ; i < _subsections.length ; i ++ )
			{
				_subsections[i].activate();
			}
		}

		private function showSubsections():void
		{
			var delay:Number = 0;
			for (var i:int = 0 ; i < _subsections.length ; i ++ )
			{
				_subsections[i].show(delay);
				delay += 0.1;
			}
		}
		
		private function hideSubsections():void
		{
			for (var i:int = 0 ; i < _subsections.length ; i ++ )
			{
				_subsections[i].hide();
				_subsections[i].deactivate();
			}
		}
		
		private function createSubsections():void
		{
			var y_gap:Number = 25;
			
			var y_pos:Number = 0;
			
			_subsections = [];
			
			for (var i:int = 0 ; i < _data.subsections.length ; i ++ )
			{
				var subsection:Subsection = new Subsection(_data.subsections[i]);
				
				subsection.deactivate();
				subsection.hide();
				
				subsection.y = y_pos;
				
				_subsections_container.addChild(subsection);
				
				y_pos += y_gap;
				
				_subsections.push(subsection);
			}
		}
	
		//private function mouseHandler(e:MouseEvent):void 
		//{
			//switch(e.type)
			//{
				//case MouseEvent.ROLL_OVER:
					//MainModel.instance.selected_section = _data.id;
					//break;
			//}
		//}
		//
		//public function startCursorPulse():void
		//{
			//_cursor.gotoAndPlay(2);
		//}
		//
		//public function stopCursorPulse():void
		//{
			//_cursor.gotoAndStop(1);
		//}
		//
		//public function get id():int
		//{
			//return _data.id;
		//}
		//
		private function drawArea(c:uint, x:Number, y:Number, w:Number, h:Number, a:Number = 1):Sprite
		{
			var area:Sprite = new Sprite();
			
			area.graphics.beginFill(c, a);
			area.graphics.drawRect(x, y, w, h); 
			area.graphics.endFill();
			
			return area
		}
		
		//
		//private static function drawGradient(w:Number, h:Number, clr1:Number , clr2:Number , alph1:Number = 1 , alph2:Number = 1):Sprite
		//{ 
			//var fill_type:String = GradientType.LINEAR;
			//var colors:Array = [ clr1, clr2 ];
			//var alphas:Array = [ alph1 , alph2 ];
			//var ratios:Array = [ 0 , h ];
			//
			//var gradient_matrix:Matrix = new Matrix();
			//gradient_matrix.createGradientBox( w, h, Math.PI / 2, 0, 0 );
			//var spreadMethod:String = SpreadMethod.PAD;
			//var fill:Sprite = new Sprite();
			//var g:Graphics = fill.graphics;
			//g.beginGradientFill( fill_type, colors, alphas, ratios, gradient_matrix, spreadMethod );
			//g.drawRect(0, 0, w, h);
			//
			//return fill;
		//}
	}

}